package com.sl.springboot.controller;

import com.sl.springboot.model.*;
import com.sl.springboot.service.CourseService;
import com.sl.springboot.service.OrdersService;
import com.sl.springboot.service.UcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class OrdersController {

    // 依赖注入
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UcardService ucardService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findOrders")
    public String findOrders(Integer pageIndex, Integer pageSize ,String cname,String uname, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        String type = (String)session.getAttribute("type");
        Map mp = new HashMap();
        mp.put("cname",cname);
        mp.put("uname",uname);
        if(type.equals("02")){
            User u = (User)session.getAttribute("ad");
            mp.put("uid",u.getId());
        }else if(type.equals("03")){
            Coach coach = (Coach) session.getAttribute("ad");
            mp.put("instructor",coach.getUsername());
        }

        PageInfo<Orders> pageList = ordersService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "OrdersList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addOrders" ,method = RequestMethod.POST)
    @ResponseBody
    public String addOrders( @RequestBody Orders orders) {
        int d = ordersService.addOrders(orders);
        return "OrdersList";
    }



    /**
     * 购买课程
     */
    @RequestMapping( "/addCoursePost")
    @ResponseBody
    public String addCardPost(String id,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "201";
        }
        User u = (User)session.getAttribute("ad");
        Course courseById = courseService.findCourseById(id);
        //判断是否买了充值卡
        Map mps = new HashMap();
        mps.put("uid",u.getId());
        List<Ucard> ucards = ucardService.queryFilter(mps);
        if(ucards.size()>0){
            Ucard ucard = ucards.get(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowDate_String = sdf.format( new Date() );
            Date nowDate_Date = sdf.parse(nowDate_String);
            Date endDate_Date = sdf.parse(ucard.getTime());
            int compareTo = nowDate_Date.compareTo(endDate_Date);
            if(compareTo == -1){
                //判断充值卡是否金额充足
                BigDecimal courseMoney = new BigDecimal(courseById.getMoney());
                BigDecimal cardMoney = new BigDecimal(ucard.getMoney());
                int flag = courseMoney.compareTo(cardMoney);
                if(flag == -1 || flag == 0){
                    BigDecimal sum = cardMoney.subtract(courseMoney);
                    ucard.setMoney(sum.toString());
                    ucardService.updateUcard(ucard);
                }else{
                    return "204";//充值卡金额不足
                }
            }else{
                return "203";//充值卡过期
            }
        }else{
            return "202";//未购充值卡
        }

        Orders orders = new Orders();
        orders.setCid(id);
        orders.setUid(u.getId());
        orders.setMoney(courseById.getMoney());
        orders.setStatus("01");
        orders.setSurplusHour(Integer.parseInt(courseById.getCourseHour()));
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        orders.setOrderTime(time);
        int d = ordersService.addOrders(orders);
        return "200";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteOrders")
    @ResponseBody
    public String deleteOrders(String id) {
        int d = ordersService.deleteOrders(id);
        return "OrdersList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateOrders")
    public String updateOrders( Orders orders) {
        int d = ordersService.updateOrders(orders);
        return "redirect:/findOrders";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findOrdersById")
    public String findOrdersById(String id,Model model,HttpServletRequest request) {
        Orders orders= ordersService.findOrdersById(id);
        model.addAttribute("orders",orders);
        return "OrdersEdit";
    }


}
